package com.hqyj.shop.controller;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.shop.pojo.PageData;
import com.hqyj.shop.pojo.ShowGoods;
import com.hqyj.shop.service.imp.ShowGoodsServiceImpl;
//import com.sun.org.apache.xpath.internal.operations.Mod;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/goods")
public class ShowGoodsController {
    @Autowired
    private ShowGoodsServiceImpl goodsService;
    @RequestMapping("/all")
    public String queryAll(ModelMap modelMap){
        List<ShowGoods> goods = goodsService.queryAll();
        for (ShowGoods one : goods){
            System.out.println(one);
        }
        modelMap.addAttribute("goods",goods);
        return "goodsShow";
    }

    @RequestMapping("/flower")
    public String queryflower(Integer pageNumber, Integer pageSize,ModelMap modelMap){
        int number;
        int size;
        if(pageNumber == null){
            number = 1;
        }else {
            number = pageNumber;
        }
        if (pageSize == null){
            size =2;
        }else {
            size = pageSize;
        }
        PageHelper.startPage(number,size);
        List<ShowGoods> goods = goodsService.queryByCategory("flower");
        PageInfo<ShowGoods> pageInfo = new PageInfo<>(goods);
        PageData<ShowGoods> pageData = goodsService.getGoodsByPage(number,size,pageInfo);
        modelMap.put("goods",pageData);
        modelMap.put("url","flower");
        return "goodsShow";
    }
    @RequestMapping("/food")
    public String queryfood(Integer pageNumber, Integer pageSize,ModelMap modelMap){
        int number;
        int size;
        if(pageNumber == null){
            number = 1;
        }else {
            number = pageNumber;
        }
        if (pageSize == null){
            size =2;
        }else {
            size = pageSize;
        }
        PageHelper.startPage(number,size);
        List<ShowGoods> goods = goodsService.queryByCategory("food");
        PageInfo<ShowGoods> pageInfo = new PageInfo<>(goods);
        PageData<ShowGoods> pageData = goodsService.getGoodsByPage(number,size,pageInfo);
        modelMap.put("goods",pageData);
        modelMap.put("url","food");
        return "goodsShow";
    }

    @RequestMapping("/getAllByPage")
    public String getAllByPage(Integer pageNumber, Integer pageSize, ModelMap modelMap){
        int number;
        int size;
        if(pageNumber == null){
            number = 1;
        }else {
            number = pageNumber;
        }
        if (pageSize == null){
            size =2;
        }else {
            size = pageSize;
        }
        PageData<ShowGoods> goods = goodsService.queryByPage(number,size);
        modelMap.put("goods",goods);
        modelMap.put("url","getAllByPage");
        return "goodsShow";
    }
    @RequestMapping(value = "/byname")
//    @ResponseBody
//    public Map<String,Object> getByName(String search){
//        List<ShowGoods> goods = goodsService.queryByName(search);
//        for (ShowGoods one:goods){
//            System.out.println(one);
//        }
//        Map<String,Object> map = new HashMap<>();
//        map.put("search",goods);
//        return map ;
//    }
    public String getByName(Integer pageNumber, Integer pageSize,String search,ModelMap modelMap){
        int number;
        int size;
        if(pageNumber == null){
            number = 1;
        }else {
            number = pageNumber;
        }
        if (pageSize == null){
            size =2;
        }else {
            size = pageSize;
        }
        PageHelper.startPage(number,size);
        List<ShowGoods> goods = goodsService.queryByName(search);
        PageInfo<ShowGoods> pageInfo = new PageInfo<>(goods);
        PageData<ShowGoods> pageData = goodsService.getGoodsByPage(number,size,pageInfo);
        modelMap.put("goods",pageData);
        modelMap.put("url","byname");
        return "goodsShow";
    }
    @RequestMapping("/lessBymoney")
    public String getGoddsByMoneyless(Integer pageNumber, Integer pageSize,String money,ModelMap modelMap){
        int number;
        int size;
        if(pageNumber == null){
            number = 1;
        }else {
            number = pageNumber;
        }
        if (pageSize == null){
            size =2;
        }else {
            size = pageSize;
        }
        PageHelper.startPage(number,size);
        double intmoney =Double.valueOf(money);
        List<ShowGoods> goods = goodsService.queryByMoneyLess(intmoney);
        PageInfo<ShowGoods> pageInfo = new PageInfo<>(goods);
        PageData<ShowGoods> pageData = goodsService.getGoodsByPage(number,size,pageInfo);
        modelMap.put("goods",pageData);
        modelMap.put("url","lessBymoney");
        modelMap.put("url2","&money="+money+"");
        return "goodsShow";
    }
    @RequestMapping("/moreBymoney")
    public String getGoddsByMoneymore(Integer pageNumber, Integer pageSize,String money,ModelMap modelMap){
        int number;
        int size;
        if(pageNumber == null){
            number = 1;
        }else {
            number = pageNumber;
        }
        if (pageSize == null){
            size =2;
        }else {
            size = pageSize;
        }
        PageHelper.startPage(number,size);
        double intmoney =Double.valueOf(money);
        List<ShowGoods> goods = goodsService.queryByMoneyMore(intmoney);
        PageInfo<ShowGoods> pageInfo = new PageInfo<>(goods);
        PageData<ShowGoods> pageData = goodsService.getGoodsByPage(number,size,pageInfo);
        modelMap.put("goods",pageData);
        modelMap.put("url","moreBymoney");
        modelMap.put("url2","&money="+money+"");
        return "goodsShow";
    }
    @RequestMapping("/goodsdetail")
    public String getgoodsdetail(int id,ModelMap modelMap){
        ShowGoods goods = goodsService.queryById(id);
        modelMap.addAttribute("goods",goods);
        return "goodsdetails";
    }
    @RequestMapping("/getByNamePage")
    public String getByNamePage(String money,Integer pageNumber, Integer pageSize, ModelMap modelMap){
        int number;
        int size;
        if(pageNumber ==null){
            number =1;
        }else number =pageNumber;
        if (pageSize == null){
            size = 2;
        }else size = pageSize;
        PageHelper.startPage(number,size);
        double intmoney =Double.valueOf(money);
        List<ShowGoods> goods = goodsService.queryByMoneyMore(intmoney);
        PageInfo<ShowGoods> pageInfo = new PageInfo<>(goods);
        PageData<ShowGoods> pageData = goodsService.getGoodsByPage(number,size,pageInfo);
        modelMap.put("goods",pageData);
        modelMap.put("url","getByNamePage");
        return "goodsShow";
    }
    @RequestMapping("/photoadd")
    @ResponseBody
    public Map<String,Object> addadd(String add,String id){
        int idi = Integer.parseInt(id);
        Map<String,Object> map = new HashMap<>();
        int i = goodsService.addadd(idi,add);
        map.put("massage","图片上传成功");
        return map;
    }
    @RequestMapping("/file")
    @ResponseBody
    public Map<String,Object> ajaxFllie(MultipartFile someFile, HttpServletRequest request){
        System.out.println(someFile);
        //保存上传的文件
        //上传目录的真实路径
        String upload = request.getServletContext().getRealPath("static/image");
        System.out.println(upload);
        File uploadDic = new File(upload);
        File uploadFile = new File(upload,someFile.getOriginalFilename());
        Map<String,Object> result =new HashMap<>();
        try {
            someFile.transferTo(uploadFile);
            result.put("message","文件上传成功");
        } catch (IOException e) {
            e.printStackTrace();
            result.put("message","文件上传失败");
        }
        return result;
    }

}
